package com.wangqingbo.myspringbootproject.config;


import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 设置国际化的语言和地区
 */
public class MessageLocaleResolver implements LocaleResolver {
    /**
     * 根据传过来的语言参数，设置国家或地区
     * @param request
     * @return
     */
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        // 接收传递过来的语言参数
        String language = request.getParameter("language");
        // 设置一个默认的语言
        Locale locale = Locale.getDefault();
        // 根据参数，重新设置语言和地区
        if (language != null && !"".equals(language) && language.contains("_"))
        {
            // 用下划线分割参数  zh_CN
            String[] languageStrs = language.split("_");
            // 重新去创建一个语言地区
            locale = new Locale(languageStrs[0], languageStrs[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
